package com.tnovoselec.android.travelassistant.adapter;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tnovoselec.android.travelassistant.R;
import com.tnovoselec.android.travelassistant.dto.IGeoPoint;

public abstract class AbstractAdapter<T extends IGeoPoint> extends BaseAdapter{
	
	protected List<IGeoPoint> items;
	protected Context context;
	protected LayoutInflater inflater;

	public AbstractAdapter(Context context) {
		super();
		this.context = context;
	}

	@Override
	public int getCount() {
		return items != null ? items.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v;
		if (convertView != null) {
			v = convertView;
		} else {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.bankomati_list_item, parent, false);
		}

		IGeoPoint item = items.get(position);
		TextView title = (TextView) v.findViewById(R.id.list_item_title);
		TextView desc = (TextView) v.findViewById(R.id.list_item_description);
		ImageView logo = (ImageView) v.findViewById(R.id.list_item_logo);

		if (title != null) {
			title.setText(item.getAdresa());
		}
		if (desc != null) {
			desc.setText(item.getBank_id());
		}
		if (logo != null) {
			logo.setImageResource(getLogoImageResource(item));
		}
		return v;
	}

	public void setData(List<IGeoPoint> items) {
		this.items = items;
		Collections.sort(this.items);
		notifyDataSetChanged();
	}
	
	protected abstract int getLogoImageResource(IGeoPoint item);

}
